package top.gbyun.core.node;

import akka.actor.ActorContext;
import top.gbyun.core.ActorSystemContext;
import top.gbyun.core.api.NodeContext;
import top.gbyun.core.entity.FlowInfo;
import top.gbyun.core.entity.NodeInfo;
import top.gbyun.core.msg.NodeMsg;
import top.gbyun.core.msg.NodeToFlowTellNextMsg;

/**
 * @author gengbin
 * @date 2024-12-26-10:58
 */
public class DefaultNodeContext implements NodeContext {
    private final ActorSystemContext mainCtx;
    private final FlowInfo flowInfo;

    private final NodeInfo nodeInfo;
    private final ActorContext actorContext;

    public DefaultNodeContext(ActorSystemContext mainCtx, FlowInfo flowInfo, NodeInfo nodeInfo, ActorContext actorContext) {
        this.mainCtx = mainCtx;
        this.flowInfo = flowInfo;
        this.nodeInfo = nodeInfo;
        this.actorContext = actorContext;
    }


    /**
     * Sends message to all Rule Nodes in the Rule Chain
     * that are connected to the current Rule Node using specified relationType.
     *
     * @param msg
     */
    @Override
    public void send(NodeMsg msg) {
        NodeToFlowTellNextMsg nodeToFlowTellNextMsg = new NodeToFlowTellNextMsg(msg,nodeInfo.getId(),flowInfo.getId());
        actorContext.parent().tell(nodeToFlowTellNextMsg, actorContext.self());
    }

    /**
     * Sends message to the current Rule Node with specified delay in milliseconds.
     * Note: this message is not queued and may be lost in case of a server restart.
     *
     * @param msg
     * @param delayMs
     */
    @Override
    public void tellSelf(NodeMsg msg, long delayMs) {

    }

    /**
     * Notifies Rule Engine about failure to process current message.
     *
     * @param msg - message
     * @param th  - exception
     */
    @Override
    public void tellFailure(NodeMsg msg, Throwable th) {

    }

    @Override
    public void ack(NodeMsg engineMsg) {

    }

    @Override
    public ActorContext getActorContext() {
        return actorContext;
    }
}
